package com.zzg.controller;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zzg.easypoi.entity.Member;
import com.zzg.easypoi.util.EasyPoiUtil;



@Controller
@RequestMapping("/api/easypoi")
public class EasyPoiController {

	@RequestMapping(value="/imports",method={RequestMethod.GET})
    public void imports(HttpServletRequest request, HttpServletResponse response){
		Member one = new Member("张三", "1", new Date(), "一部", "1");
		Member two = new Member("李四", "2", new Date(), "二部", "2");
		Member three = new Member("王五", "3", new Date(), "三部", "1");
		List<Member> container = new ArrayList<Member>();
		container.add(one);
		container.add(two);
		container.add(three);
		
		String fileName="导出文件.xlsx";
		Workbook workbook = EasyPoiUtil.exportExcel(container, "导出测试", "sheet", Member.class);
		 try {
	            response.setCharacterEncoding("UTF-8");
	            response.setHeader("content-Type", "application/vnd.ms-excel");
	            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
	            workbook.write(response.getOutputStream());
	        } catch (IOException e) {
	            throw new  RuntimeException(e);
	        }
    }
}
